/* QAD_WipStatusReport.p - REPETITIVE WIP STATUS REPORT                       */
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: QAD_TransactionsAccountingReport.p 1046 2010-09-10 09:08:19Z jvz  $: */
/*                                                                            */
/* Report matches legacy reworp05.p for use by QAD Reporting Framework.       */
/*                                                                            */

{mfsubdirs.i}
{{&US_BBI}mfdeclre.i}
{{&US_BBI}gplabel.i}
{{&US_WO}woconsdf.i}

{com/qad/shell/report/dsReportRequest.i}
{com/qad/shell/report/ReportConstants.i}

/*============================================================================*/
/* ****************************  TEMP TABLES  ******************************* */
/*============================================================================*/

/* TEMP-TABLE DEFINITION BLOCK */

define temp-table tt_wo_mstr no-undo like wo_mstr
   field si_desc       like si_mstr.si_desc
   field pt_desc1      like pt_mstr.pt_desc1
   field ptal_alt_part like ptal_mstr.ptal_alt_part
   field mfg_type      as   character
   field ln_desc       like ln_mstr.ln_desc
   field wostatus      as   character
   field unique_id     as   integer
.

define temp-table ttComments no-undo
   field cmmt          as   character
   field ref_unique_id like tt_wo_mstr.unique_id
.

define temp-table tt_wr_route no-undo
   field move_in       as   decimal
   field unique_id     as   integer
   field ref_unique_id like wo_mstr.unique_id
.

define temp-table tt_wld_det no-undo like wld_det
   field queue         as   character
   field ip_um         like pt_mstr.pt_um
   field ref_unique_id like tt_wr_route.unique_id
.

define temp-table ttReportOptions no-undo
   field ordertype as character
.

define dataset dsReportResults for tt_wo_mstr, ttComments, tt_wr_route, tt_wld_det, ttReportOptions.

/*============================================================================*/
/* *****************************  MAIN BLOCK  ******************************* */
/*============================================================================*/
define input parameter runReport    as logical no-undo.
define input parameter reportHandle as handle  no-undo.
define input parameter dataset      for dsReportRequest.
define output parameter dataset-handle phReportResults.

{com/qad/shell/report/reporting.i}

define variable vhDS as handle    no-undo.
define variable order_type_key   like lngd_key1 initial "0" no-undo.
define variable c-lngd-dataset   like lngd_det.lngd_dataset no-undo
                                 initial "wo_mstr".
define variable orderType        as character no-undo.

empty temp-table tt_wo_mstr      no-error.
empty temp-table ttComments      no-error.
empty temp-table tt_wr_route     no-error.
empty temp-table tt_wld_det      no-error.
empty temp-table ttReportOptions no-error.

for first ttReportRequest no-lock:

   vhDS = dataset dsReportResults:handle.
   run FillMetaData in reportHandle(
      vhDS, "QAD_WipStatusReport.meta.xml").

   run SetReportMetaData (input reportHandle).

   if runReport then do:
      run RunReport
         (output dataset-handle phReportResults).
   end.

end.

PROCEDURE SetReportMetaData:

   define input parameter  ipReportHandle as handle.

   run SetFieldMetaParameter in ipReportHandle("tt_wo_mstr",
         "wo_site", "DefaultValue", global_site).
   
   for first lngd_det where lngd_dataset = c-lngd-dataset
      and lngd_field   = "orderType"
      and lngd_lang    = global_user_lang
      and lngd_key1    = order_type_key
   no-lock:
   end. /* for first lngd_det */

   if available lngd_det
   then do:
      orderType = lngd_key2.
   end. /* if available lngd_det */
   else do:
      orderType = "".
   end. /* else do */

   run SetFieldMetaParameter in ipReportHandle(
      "ttReportOptions", "ordertype", "DefaultValue", orderType).

END PROCEDURE. /* PROCEDURE ModifyMetaData */

/* DATA RETRIEVING LOGIC BLOCK */
PROCEDURE RunReport:

   define output parameter dataset-handle phReportResults.

   /* RETRIEVE THE DATA FROM DATABASE */
   define variable queryString         as character no-undo.
   define variable hMainQuery          as handle    no-undo.
   define query    MainQuery           for wo_mstr,ptal_mstr,pt_mstr,pl_mstr.
   define buffer   womstr              for wo_mstr.
   define variable unique_id           as integer no-undo.
   define variable unique_route_id     as integer no-undo.
   define variable wostatus            as character no-undo.
   define variable i                   as integer no-undo.
   define variable mfg_type            as character no-undo.
   define variable first_op            like mfc_ctrl.mfc_logical no-undo.
   define variable moved_in            like wr_route.wr_qty_cummove no-undo.
   define variable h_wiplottrace_funcs as handle no-undo.

   assign
      hMainQuery           = query MainQuery:handle
      orderType            =  GetFilterValue( "ttReportOptions",
                                              "ordertype",
                                              orderType)
   .

   {{&US_WL}wlfnc.i} /* FUNCTION FORWARD DECLARATIONS */

   for first lngd_det where
      lngd_dataset      = c-lngd-dataset
      and lngd_field    = "orderType"
      and lngd_lang     = global_user_lang
      and lngd_key2     = orderType
   no-lock:
   end. /* for first lngd_det */

   if not available lngd_det
   then do:
      /* MUST BE (S)cheduled or (D)iscrete or (B)oth */
      {{&US_BBI}pxmsg.i &MSGNUM=10346 &ERRORLEVEL=4}
      return.
   end. /* if not available lngd_det */

   order_type_key = lngd_key1.

   queryString = "for each wo_mstr no-lock where wo_domain = "
                  + quoter(global_domain).

   run AddAllConditions in reportHandle
      ("wo_mstr", input-output queryString).

   queryString = queryString + " and (( "
                  + quoter(wo_mfg_type) + " = " + quoter({&MFG_TYPE_DISCRETE})
                  + " and wo_type <> 'S' " + " and ( "
                  + quoter(order_type_key) + " = "
                  + quoter({&MFG_TYPE_DISCRETE})
                  + " or " + quoter(order_type_key) + " = "
                  + quoter({&MFG_TYPE_DISCRETE_AND_REPETITIVE}) + " )) "
                  + " or ( " + quoter(wo_mfg_type)
                  + " = " + quoter({&MFG_TYPE_REPETITIVE})
                  + " and wo_type <> 'S' " + " and ( "
                  + quoter(order_type_key) + " = "
                  + quoter({&MFG_TYPE_REPETITIVE})
                  + " or " quoter(order_type_key) + " = "
                  + quoter({&MFG_TYPE_DISCRETE_AND_REPETITIVE}) + " ))) "
                  + " use-index wo_type_part, "
                  + " each pt_mstr no-lock "
                  + " where pt_mstr.pt_domain = "
                  + quoter(global_domain)
                  + " and pt_part = wo_part, "
                  + " each pl_mstr no-lock "
                  + " where pl_mstr.pl_domain = "
                  + quoter(global_domain)
                  + " and pl_prod_line = pt_prod_line "
                  + " by wo_type by wo_site by wo_part "
                  + " by wo_line by wo_due_date by wo_lot: ".

   hMainQuery:query-prepare(queryString).
   hMainQuery:query-open().
   hMainQuery:get-next().

   repeat while not hMainQuery:query-off-end:

      for first womstr where womstr.wo_domain = global_domain
         and womstr.wo_lot = wo_mstr.wo_lot
      no-lock:
      end. /* for first womstr */

      for first si_mstr where si_mstr.si_domain = global_domain
         and si_site = womstr.wo_site
      no-lock:
      end. /* for first si_mstr */

      for first pt_mstr where pt_mstr.pt_domain = global_domain
         and pt_part = womstr.wo_part
      no-lock:
      end. /* for first pt_mstr */

      for first ln_mstr where ln_mstr.ln_domain = global_domain
         and ln_site = womstr.wo_site
         and ln_line = womstr.wo_line
      no-lock:
      end. /* for first ln_mstr */

      for first ptal_mstr where ptal_mstr.ptal_domain = global_domain
         and ptal_part = womstr.wo_part
         and ptal_active
      no-lock:
      end. /* for first ptal_mstr */

      if womstr.wo_status = "C"
      then do:
         wostatus = getTermLabel("CLOSED",20).
      end. /* if womstr.wo_status */
      else do:
         if womstr.wo_due_date < today
         then do:
            wostatus = getTermLabel("EXPIRED",20).
         end. /* if womstr */
         else do:
            wostatus = getTermLabel("ACTIVE",20).
         end. /* else do */
      end. /* else do */

      for first lngd_det where lngd_dataset = "wo_mstr"
         and lngd_field = "orderType"
         and lngd_lang  = global_user_lang
         and lngd_key1  = womstr.wo_mfg_type
      no-lock:
         mfg_type = lngd_key2.
      end. /* for first lngd_det */

      unique_id = unique_id + 1.
      create tt_wo_mstr.
      buffer-copy womstr to tt_wo_mstr no-error.
      assign
         tt_wo_mstr.si_desc         = si_mstr.si_desc when (available si_mstr)
         tt_wo_mstr.pt_desc1        = pt_mstr.pt_desc1 when (available pt_mstr)
         tt_wo_mstr.ptal_alt_part   = ptal_mstr.ptal_alt_part
                                      when available(ptal_mstr)
         tt_wo_mstr.mfg_type        = mfg_type
         tt_wo_mstr.ln_desc         = ln_mstr.ln_desc when (available ln_mstr)
         tt_wo_mstr.wostatus        = wostatus
         tt_wo_mstr.unique_id       = unique_id
      .

      for each cmt_det no-lock
         where cmt_det.cmt_domain = global_domain
         and cmt_indx = wo_cmtindx
         and cmt_indx > 0:
         do i = 1 to 15:
            if cmt_cmmt[i] <> ""
            then do:
               create ttComments.
               assign
                  ttComments.cmmt            = cmt_cmmt[i]
                  ttComments.ref_unique_id   = unique_id
               .
            end. /* if cmt_cmmt */
         end. /* do i */
      end. /* for each cmt_det */



      for each wr_route no-lock
         where wr_route.wr_domain = global_domain
         and   wr_lot = wo_lot
      break by wr_op:

         first_op = first(wr_op).
         create tt_wr_route.
         buffer-copy wr_route to tt_wr_route no-error.
         assign
            tt_wr_route.move_in        = move_in
            tt_wr_route.unique_id      = unique_route_id
            tt_wr_route.ref_unique_id  = unique_id
         .

         if is_wiplottrace_enabled()
         then do:
               {{&US_BBI}gprun.i ""rewoiqa.p""
                        "(input wr_lot,
                          input wr_op,
                          input pt_um)"}\
            define variable dummy        as character no-undo.
            define variable routing_code as character no-undo.
            define variable effdate      as date no-undo.
            i = 0.
            {{&US_WL}wlcon.i}
            for each wld_det no-lock
               where wld_det.wld_domain = global_domain
               and wld_alloc_id         = wr_lot
               and wld_alloc_op         = wr_op
               and wld_qty              <> 0:

               {{&US_GP}gplngn2a.i
                  &file='wld_det'
                  &field='wld_alloc_que'
                  &code=wld_alloc_que
                  &mnemonic=queue
                  &label=dummy}

               create tt_wld_det.
               buffer-copy wld_det to tt_wld_det no-error.
               assign
                  tt_wld_det.queue = queue
                  tt_wld_det.ip_um = pt_um
                  tt_wld_det.ref_unique_id = unique_route_id
               .

            i = i + 1.
            end. /* FOR EACH wld_det */

            {{&US_BBI}gprun.i ""wlgeteff.p""
                     "(output effdate) no-error"}

            if effdate = ?
            then
               effdate = today.

            for first wo_mstr
               where wo_mstr.wo_domain = global_domain
               and   wo_lot            = wr_lot
            no-lock:
            end. /* FOR FIRST wo_mstr */

            routing_code = if wo_routing = ''
                           then
                              wo_part
                           else
                              wo_routing.

            for last wlrm_mstr
               where wlrm_mstr.wlrm_domain =  global_domain
               and  (wlrm_routing          =  routing_code
               and (wlrm_start            <= effdate or
                    wlrm_end               =  ?)
                   )
               no-lock:
            end. /* FOr LAST wlrm_mstr */

            if not available wlrm_mstr
            then
               for first wlrm_mstr
                  where wlrm_mstr.wlrm_domain = global_domain
                  and  (wlrm_routing          = routing_code
                  and   wlrm_start            = ?
                  and  (wlrm_end             >= effdate or
                        wlrm_end              = ?)
                       )
                  no-lock:
               end.

            if available wlrm_mstr
            then do:
               if wlrm_trc_parent and i = 0 then do:
                  /* NO WIP LOTS FOUND FOR THIS OPERATION */
                  {{&US_BBI}pxmsg.i &MSGNUM=8425 &ERRORLEVEL=1}
               end. /* IF wlrm_trc_parent */
            end. /* IF AVAILABLE wlrm_mstr */
            else do:
               for first wlc_ctrl
                  where wlc_ctrl.wlc_domain = global_domain
               no-lock:
               end.
               if wlc_trc_parent
                   and i = 0
               then do:
                  /* NO WIPLOTS FOUND FOR THIS OPERATION */
                  {{&US_BBI}pxmsg.i &MSGNUM=8425 &ERRORLEVEL=1}

               end. /* IF wlc_trc_parent */
            end. /* IF NOT AVAILABLE wlrm_mstr */
         end. /* if is_wiplottrace_enabled */

         moved_in = wr_qty_cummove.
      end. /* for each wr_route */

      hMainQuery:get-next().
   end. /* REPEAT QUERY */

   hMainQuery:query-close().
   delete object hMainQuery.
   phReportResults = dataset dsReportResults:handle.

END PROCEDURE.
